namespace AsyncFactory
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Reflection;
    using System.Windows.Forms;

    internal class FrmLoading : Form
    {
        private IContainer components;
        private EventHandler evtHandler;
        private Image m_img;

        public FrmLoading(LoadingImageType imgType)
        {
            this.InitializeComponent();
            base.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
            base.UpdateStyles();
            this.evtHandler = new EventHandler(this.OnImageAnimate);
            this.m_img = Image.FromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream("AsyncFactory.Resources." + imgType.ToString() + ".gif"));
            base.Width = this.m_img.Width;
            base.Height = this.m_img.Height;
            this.BeginAnimate();
        }

        private void BeginAnimate()
        {
            if (this.m_img != null)
            {
                ImageAnimator.Animate(this.m_img, this.evtHandler);
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            base.SuspendLayout();
            base.AutoScaleDimensions = new SizeF(6f, 12f);
            base.AutoScaleMode = AutoScaleMode.Font;
            this.BackColor = Color.Red;
            this.BackgroundImageLayout = ImageLayout.None;
            base.ClientSize = new Size(0xfd, 0x1f);
            base.FormBorderStyle = FormBorderStyle.None;
            base.Name = "FrmLoading";
            base.ShowIcon = false;
            base.ShowInTaskbar = false;
            base.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "FrmLoading";
            base.TopMost = true;
            base.TransparencyKey = Color.Red;
            base.ResumeLayout(false);
        }

        private void OnImageAnimate(object sender, EventArgs e)
        {
            base.Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            if (this.m_img != null)
            {
                this.UpdateImage();
                e.Graphics.DrawImage(this.m_img, new Rectangle(0, 0, this.m_img.Width, this.m_img.Height));
            }
        }

        private void StopAnimate()
        {
            this.m_img = null;
            ImageAnimator.StopAnimate(this.m_img, this.evtHandler);
        }

        private void UpdateImage()
        {
            ImageAnimator.UpdateFrames(this.m_img);
        }
    }
}

